package mo.org.cm309.shoppingcart.servlet.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mo.org.cm309.shoppingcart.dao.ProductDAO;
import mo.org.cm309.shoppingcart.entity.Product;
import mo.org.cm309.shoppingcart.service.ProductService;
import mo.org.cm309.shoppingcart.utility.CommonUtility;

public class Producttable extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try{
			
			List<Product> lista=ProductDAO.getInstance().findByWhereClause(" ORDER BY id "); 
		
	long id =0;
	long pid=0;
	String pname=null;
//	Product lista;
	ProductService productService;
	Product product;
	PrintWriter writer= resp.getWriter();
	writer.print("<html>");
	for ( Product pp : lista) {

			 id = pp.getId();
//			pid  = CommonUtility.getLongByString(id);
			
			//GET PRODUCT SERVICE
			 productService= new ProductService();
			//GET PRODUCT BY ID
			product = productService.getProductById(id);
			//ADD PRODUCT STOCK
			pname=product.getProductname();
			//product = productService.setStock(product, 1);
			//UPDATE NEW PRODUCT TO DATABASE
			//productService.update(product);
			
			//IF ACTION SUCCESSFUL, RETURN A SUCCESS PAGE
			
			
			writer.print("product id: " + id + " is producta : <a href=\"productDetailServlet?productId="+id+"\">" + pname + "</a><br />");
			}
			writer.print("</html>"); 
		}
	 catch(Exception e){
	 e.printStackTrace();
	 //IF FOUND EXCEPTION, RETURN A FAILURE PAGE
	 PrintWriter writer = resp.getWriter();
	 writer.print("<html>");
	 writer.print("ERROR! Update product stock failure!");
	 writer.print("</html>");
	 }


}
}
